package kr.saeildamil.androidforstudy2.ch28;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DownloadingImage extends Activity {
	Button mBtn;
	Button mBtnEnQuene;
	ImageView mImage;
	DownloadManager mDm;
	long mId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mDm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		mBtn = new Button(this);
		mBtn.setText("Download");
		mBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				(new DownThread("http://developer.android.com/images/home/android-jellybean.png")).start();
			}
		});
		
		mBtnEnQuene = new Button(this);
		mBtnEnQuene.setText("Download");
		mBtnEnQuene.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("http://developer.android.com/images/home/design.png");
				DownloadManager.Request request = new DownloadManager.Request(uri);
				request.setTitle("Downloading Test");
				request.setDescription("Downloading the Image");
				request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
				
				mId =mDm.enqueue(request);
				
				// DownloadManager 에서 BR 방송 실시;
				IntentFilter filter = new IntentFilter();
				filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
				registerReceiver(receiver, filter);
			}
		});

		mImage = new ImageView(this);

		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);

		layout.addView(mBtn, params);
		layout.addView(mImage, params);
		layout.addView(mBtnEnQuene, params);

		setContentView(layout);

	}
	
	protected void onPause() {
		if(mId != 0){
			unregisterReceiver(receiver);
			mId = 0;
		}
	};
	
	BroadcastReceiver receiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(DownloadingImage.this, "Download completed!", Toast.LENGTH_SHORT).show();
		}
	};
	
	class DownThread extends Thread{
		String mAddr;
		
		public DownThread(String mAddr){
			super();
			this.mAddr = mAddr;
		}

		@Override
		public void run() {
			super.run();
			InputStream is = null;
			
			try {
				is = new URL(mAddr).openStream();
				Bitmap bit = BitmapFactory.decodeStream(is);
				Message msg = mAfterDown.obtainMessage();
				msg.obj = bit;
				mAfterDown.sendMessage(msg);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {try {
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
			}}
		}	
	}
	
	Handler mAfterDown = new Handler(){
		public void handleMessage(Message msg) {
			Bitmap bit = (Bitmap) msg.obj;
			if(bit == null){
				Toast.makeText(DownloadingImage.this, "Bitmap is null!", Toast.LENGTH_SHORT).show();
			}else{
				mImage.setImageBitmap(bit);
			}
		}
	};
}
